app.run(['$location', '$rootScope', '$state', '$http', '$q', 'dialog',
    function($location, $rootScope, $state, $http, $q, dialog) {

        $rootScope.logout = function() {
            $http.get('/api/logout')
                .success(function(res) {
                    $rootScope.login = false;
                    $state.go('login');
                })
                .error(function(res) {
                    dialog.showMessage({ msg: '网络错误！' });
                });
        };

        $rootScope.$on('$stateChangeStart', function(event, toState, toParams, fromState, fromParams) {
            if (toState.name !== 'login') {
                $http.get('/api/checkLogin')
                    .success(function(res) {
                        $rootScope.login = true;
                    })
                    .error(function(res) {
                        $rootScope.login = false;
                        $state.go('login');
                    });
            } else {
                //已登录则跳转至列表页
                $http.get('/api/checkLogin')
                    .success(function(res) {
                        $rootScope.login = true;
                        $state.go('articleList');
                    })
                    .error(function(res) {
                        $rootScope.login = false;
                    });;
            }
        });

        $rootScope.$on('$stateChangeSuccess', function(event, toState, toParams, fromState, fromParams) {
            $rootScope.title = 'ZFH的小站 - 后台管理' + (toState.title ? ' - ' + toState.title : '');
        });
    }
]);

app.controller('loginCtrl', ['$rootScope', '$scope', '$http', '$state', 'dialog',
    function($rootScope, $scope, $http, $state, dialog) {
        $scope.login = function() {
            $http.post('/api/login', { user: $scope.user })
                .success(function(res) {
                    $rootScope.login = true;
                    $state.go('articleList');
                })
                .error(function(res) {
                    dialog.showMessage({ msg: res && res.error ? res.error : '网络错误！' });
                });
        };
    }
]);

app.controller('articleListCtrl', ['$scope', '$http', '$state', 'dialog',
    function($scope, $http, $state, dialog) {
        var vm = this;
        $scope.query = { size: 15, page: 1 };

        //查询
        vm.ArticleQuery = function() {
            $scope.query.page = 1;
            vm.getArticle();
        };

        vm.pageChanged = function() {
            vm.getArticle();
        };

        vm.getArticle = function() {
            $http.get('/api/articles?' + app.setUrlParam($scope.query))
                .success(function(res) {
                    vm.count = res.data.count || vm.count || 0;
                    vm.articles = res.data;
                })
                .error(function(res) {
                    dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                });
        };

        $scope.del = function(article, index) {
            dialog.confirm({
                msg: '确认要删除吗？',
                cb: function() {
                    $http.delete('/api/articles/' + article.id)
                        .success(function(res) {
                            vm.articles.rows.splice(index, 1);
                        })
                        .error(function(res) {
                            dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                        });
                }
            });
        };

        $scope.createFirstList = function() {
            $http.post('/api/createFirstList')
                .success(function(res) {
                    dialog.showMessage({ msg: '首页文章列表生成成功' });
                })
                .error(function(res) {
                    dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                });

        };

        vm.pageChanged();
    }
]);

app.controller('articleEditCtrl', ['$scope', '$http', '$state', '$stateParams', 'dialog',
    function($scope, $http, $state, $stateParams, dialog) {
        var vm = this;
        $scope.id = $stateParams.id;
        $http.get('/api/categorys')
            .success(function(res) {
                vm.categorys = res.data;
                vm.article = {
                    deliverTime: new Date()
                };
                if ($scope.id) {
                    $http.get('/api/articles/' + $scope.id)
                        .success(function(res) {
                            if (res.data) {
                                res.data.deliverTime = new Date(res.data.deliverTime);
                                vm.article = res.data;
                            } else {
                                dialog.showMessage({
                                    msg: '文章已被删除！',
                                    op: function() {
                                        $state.go('articleList');
                                    }
                                })
                            }
                        })
                        .error(function(res) {
                            $state.go('articleList');
                        });
                }
            })
            .error(function(res) {
                vm.categorys = [];
            });

        $scope.set = function() {
            console.log(vm.article);
            if (!$scope.id) {
                $http.post('/api/articles', vm.article)
                    .success(function(res) {
                        dialog.showMessage({
                            msg: '保存成功',
                            cb: function() {
                                $state.go('articleList');
                            }
                        });
                    })
                    .error(function(res) {
                        dialog.showMessage({
                            msg: res.error || '网络错误'
                        });
                    });
            } else {
                $http.put('/api/articles/' + $scope.id, vm.article)
                    .success(function(res) {
                        dialog.showMessage({
                            msg: '保存成功',
                            cb: function() {
                                $state.go('articleList');
                            }
                        });
                    })
                    .error(function(res) {
                        dialog.showMessage({
                            msg: res.error || '网络错误'
                        });
                    });
            }
        };

        $scope.cancel = function() {
            dialog.confirm({
                msg: '确认取消编辑吗？',
                cb: function() {
                    $state.go('articleList');
                }
            });
        };
    }
]);

app.controller('categoryListCtrl', ['$scope', '$http', '$state', 'dialog',
    function($scope, $http, $state, dialog) {
        var vm = this;

        vm.getCategory = function() {
            $http.get('/api/categorys')
                .success(function(res) {
                    vm.categorys = res.data;
                })
                .error(function(res) {
                    dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                });
        };

        $scope.del = function(category, index) {
            dialog.confirm({
                msg: '确认要删除吗？',
                cb: function() {
                    $http.delete('/api/categorys/' + category.id)
                        .success(function(res) {
                            vm.categorys.splice(index, 1);
                        })
                        .error(function(res) {
                            dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                        });
                }
            });
        };

        $scope.createMenu = function() {
            $http.post('/api/createMenu')
                .success(function(res) {
                    dialog.showMessage({ msg: '首页菜单生成成功' });
                })
                .error(function(res) {
                    dialog.showMessage({ msg: res && res.error ? res.error : '网络错误' });
                });

        };

        vm.getCategory();

    }
]);

app.controller('categoryEditCtrl', ['$scope', '$http', '$state', '$stateParams', 'dialog',
    function($scope, $http, $state, $stateParams, dialog) {
        var vm = this;
        $scope.id = $stateParams.id;
        if ($scope.id) {
            $http.get('/api/categorys/' + $scope.id)
                .success(function(res) {
                    if (res.data) {
                        vm.category = res.data;
                    } else {
                        dialog.showMessage({
                            msg: '栏目已被删除！',
                            op: function() {
                                $state.go('categoryList');
                            }
                        })
                    }
                })
                .error(function(res) {
                    $state.go('categoryList');
                });
        }

        $scope.set = function() {
            if (!$scope.id) {
                $http.post('/api/categorys', vm.category)
                    .success(function(res) {
                        dialog.showMessage({
                            msg: '保存成功',
                            cb: function() {
                                $state.go('categoryList');
                            }
                        });
                    })
                    .error(function(res) {
                        dialog.showMessage({
                            msg: res.error || '网络错误'
                        });
                    });
            } else {
                $http.put('/api/categorys/' + $scope.id, vm.category)
                    .success(function(res) {
                        dialog.showMessage({
                            msg: '保存成功',
                            cb: function() {
                                $state.go('categoryList');
                            }
                        });
                    })
                    .error(function(res) {
                        dialog.showMessage({
                            msg: res.error || '网络错误'
                        });
                    });
            }
        };

        $scope.cancel = function() {
            dialog.confirm({
                msg: '确认取消编辑吗？',
                cb: function() {
                    $state.go('categoryList');
                }
            });
        };
    }
]);